package com.photoeditor.demo.model.store.module.preload;


import android.os.Environment;
import android.text.TextUtils;

import com.common.base.ComponentContext;
import com.common.base.utils.ProcessUtils;
import com.photoeditor.demo.app.TestUser;
import com.photoeditor.R;
import com.photoeditor.demo.model.preference.ChildProcessBroadcastReceiver;
import com.photoeditor.demo.model.store.module.BaseOnlineManager;
import com.photoeditor.demo.model.store.module.StoreOnlineBean;
import com.photoeditor.demo.util.FileUtil;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by edward on 17-10-17.
 */

class PreloadOnlineDataManager extends BaseOnlineManager implements ChildProcessBroadcastReceiver.OnMainProcessMsgListener {
    private static final String URL = "superpro-soft/photoeditordemo/preload-v1";
    private static final String TEST_URL = "superpro-soft/photoeditordemo/preload-test-v1";
    private static final String FILE_NAME = "preload";

    public PreloadOnlineDataManager() {
        super();
        ChildProcessBroadcastReceiver.getInstance().registerListener(this);
    }

    @Override
    protected BaseOnlineManager.Config getConfig() {
        return new ConfigBuilder(URL, FILE_NAME)
                .baseTestRequestUrl(TEST_URL)
//                .requestInterval((int) (TimeConstant.ONE_DAY))
                .build();
    }


    @Override
    protected synchronized void notifyDataListener(boolean needUpdate) {
        super.notifyDataListener(needUpdate);
        if (needUpdate && ProcessUtils.isMainProcess(ComponentContext.getContext())) {
            ChildProcessBroadcastReceiver.getInstance().sendBroadcastMsg(ChildProcessBroadcastReceiver.TYPE_REFRESH_PRELOAD_DATA);
        }
    }

    @Override
    public void onMainProcessMsgReceive(int type, int status, Serializable data) {
        if (type == ChildProcessBroadcastReceiver.TYPE_REFRESH_PRELOAD_DATA) {
            loadData();
            notifyDataListener(true);
        }
    }

    @Override
    protected StoreOnlineBean verifyDataVersion(StoreOnlineBean data) {
        if (data == null) {
            return data;
        }
        ArrayList<StoreOnlineBean> child = data.getChildModules();
        Iterator<StoreOnlineBean> it = child.iterator();
        while (it.hasNext()) {
            StoreOnlineBean item = it.next();
            if (item.getVersioncode() > getSupportVersionCode()) {
                it.remove();
            } else {
                if (item.getChildModules() != null) {
                    // 递归删除子item
                    verifyDataVersion(item);
                }
            }
        }
        return data;
    }

    private static int getSupportVersionCode() {
        return ComponentContext.getContext().getResources().getInteger(R.integer.preload_version);
    }

    @Override
    protected boolean parse(String text) {
        if (TestUser.isTestUser()) {
            // 如果是测试买量用户，尝试读取本地存储卡根目录下json文件，用于测试
            String testFile = Environment.getExternalStorageDirectory() + File.separator + FILE_NAME;
            String testData = FileUtil.readFileToString(testFile);
            if (!TextUtils.isEmpty(testData)) {
                return super.parse(testData);
            } else {
                return super.parse(text);
            }
        } else {
            return super.parse(text);
        }
    }
}
